from urllib.parse import quote
from base64 import  b64encode

# 创建客户端     
client = HttpClient('127.0.0.1', # 目标地址:端口
                    timeout=10   # 超时时间，单位秒
                   ) 

username = 'byhy'
password = '88888888'
up = b64encode(quote(username+'#$%'+password).encode())

response = client.post(
    '/api/mgr/signin',
    # 通过data传入指定urlencode格式的消息体参数
    data={'up':up})

# 获取消息头Content-Type值
jwt = response.headers['jwt']    


#顾客账户整备
for i in range(3000):
    response = client.post(
        '/api/mgr/users',
        # 通过json传入指定json格式的消息体参数
        json={
            "action":"add_customer",
            "data":{
                "username": f"user_{i}",
                "password": "88888888",
                "realname": f"仁医集团第{i}附属医院",
                "desc":     f"第{i}城",
                "phone": f"{i}-0001"    }},
        headers={'Authorization':jwt})
    
    retcode = response.json()['retcode']
        
    if retcode != 0:
        Stats.oneError()  # 报告一个错误，加入到统计信息中 
        TestLogger.write(f'user_{i} 创建错误') 
    else:
        print(f"用户  {response.json()['id']}  创建完毕")
        


#代理商账户整备
for i in range(2000):
    response = client.post(
        '/api/mgr/users',
        # 通过json传入指定json格式的消息体参数
        json={
            "action":"add_distributor",
            "data":{
                "username": f"sell_{i}",
                "password": "88888888",
                "realname": f"销售_{i}",
                "desc":     f"第{i}代理",
                "phone": f"{i}-0002"    }},
        headers={'Authorization':jwt})
    
    retcode = response.json()['retcode']
        
    if retcode != 0:
        Stats.oneError()  # 报告一个错误，加入到统计信息中 
        TestLogger.write(f'sell_{i} 创建错误') 
    else:
        print(f"销售  {response.json()['id']}  创建完毕")
    


#药品整备
for i in range(1000):
    response = client.post(
        '/api/mgr/medicines',
        # 通过json传入指定json格式的消息体参数
        json={
            "action":"add_one",
            "data":{
            "name": f"青霉素_{i}",
            "desc": f"青霉素 国字{i}号",
            "sn": f"09987_{i}"}},
        headers={'Authorization':jwt})
    
    retcode = response.json()['retcode']
        
    if retcode != 0:
        Stats.oneError()  # 报告一个错误，加入到统计信息中 
        TestLogger.write(f'user_{i} 创建错误') 
    else:
        print(f"药品  {response.json()['id']}  创建完毕")
     

  
        







client.proxy('127.0.0.1:8888')
